'use client';

import { useQuery } from '@tanstack/react-query';
import type { IPermissionDetails } from '@/interfaces';
import { queryPermissionDetails } from '@/services/api';
import Alert from '@/app/[locale]/alert/alert';
import { useContext } from 'react';
import { PermissionAdminContext } from '@/contexts/admin/auth/permission';
import AlertLoad from '@/app/[locale]/alert/load';
import QueryPermissionInfo from '@/app/[locale]/admin/auth/permission/detail/info';
import QueryPermissionUsers from '@/app/[locale]/admin/auth/permission/detail/users';
import QueryPermissionRoles from '@/app/[locale]/admin/auth/permission/detail/roles';

export default function DetailsPermissionAdminPage(this: any) {
  const context = useContext(PermissionAdminContext);

  const queryPermissionDetailsQuery = useQuery(
    ['/permissions', context.detailItem?.id],
    async (context) => {
      return (await queryPermissionDetails({
        id: context.queryKey[1] + '',
      })) as IPermissionDetails;
    },
  );

  if (queryPermissionDetailsQuery.data) {
    const data = queryPermissionDetailsQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <QueryPermissionInfo details={data} />

            <div className="row">
              <div className="col">
                <QueryPermissionUsers details={data} />
              </div>
              <div className="col">
                <QueryPermissionRoles details={data} />
              </div>
            </div>

            <div></div>
          </div>
        </div>
      </div>
    );
  }

  if (queryPermissionDetailsQuery.error) {
    return <Alert type="error" message={queryPermissionDetailsQuery.error} />;
  }

  return <AlertLoad />;
}
